package com.blocks.blocks;

import com.blocks.ScriptBlocksApplication;
import com.blocks.controller.BlockActionListener;
import com.blocks.script.ScriptUtils;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Cylinder;

public class MotorBlock extends Block {
	public int axis = 2;

	public MotorBlock() {

	}

	public MotorBlock(String name, Long masterId) {
		super(name, masterId);
	}

	@Override
	public void init() {
		setTextureRow(1.0f);
		setTextureColumn(1.0f);
		super.init();

	}

	@Override
	public void updateLogicalState(float tpf) {
		super.updateLogicalState(tpf);
		if (isPowered()) {
			switch (getAxis()) {
			case 0:
				setColor(ColorRGBA.Red);
				break;
			case 1:
				setColor(ColorRGBA.Orange);
				break;
			case 2:
				setColor(ColorRGBA.Yellow);
				break;
			case 3:
				setColor(ColorRGBA.Green);
				break;			
			case 4:
				setColor(ColorRGBA.Blue);
				break;	
			default:
				setColor(ColorRGBA.Magenta);
				break;
			}
			MasterBlock masterBlock = ScriptBlocksApplication.getInstance()
					.getMasterBlock(this.getMasterId());
			float force = this.getFrequency(this.getBlockSides()) * tpf;
			Vector3f moveto = Vector3f.ZERO;
			if(axis<=2){
			    moveto = masterBlock.getLocalTranslation().add(
					(this.getLocalRotation().getRotationColumn(axis%3)
							.mult(force)));
			}else{
				moveto = masterBlock.getLocalTranslation().subtract(
						(this.getLocalRotation().getRotationColumn(axis%3)
								.mult(force)));
			}

			masterBlock.setLocalTranslation(moveto);
		} else {
			setColor(ColorRGBA.Gray);
		}

	}

	@Override
	public void use() {
		axis = (axis + 1) % 6;
	}

	public int getAxis() {
		return axis;
	}

	public void setAxis(int axis) {
		this.axis = axis;
	}

}
